package com.example.mediator;

import com.example.mediator.bo.User;

import java.util.LinkedList;
import java.util.List;

public class ChatRoomMediatorImpi implements Mediator{
    private List<User> users = new LinkedList<User>();
    private String roomName;

    public ChatRoomMediatorImpi(String roomName) {
        this.roomName = roomName;
    }

    @Override
    public String getRoomName() {
        return roomName;
    }

    @Override
    public void addUser(User user) {
        users.add(user);
        System.out.println(user.getName() + " 加入了群聊 " + roomName);
    }

    @Override
    public void removeUser(User user) {
        users.remove(user);
        System.out.println("--" + user.getName() + " 退出了群聊--");
    }

    // 私发
    @Override
    public void sendPrivateMessage(String name, String receiveName, String message) {
        for (User user : users) {
            if (user.getName().equalsIgnoreCase(receiveName)) {
                    user.receive(name, message);
            }
        }
    }

    @Override
    public void sendMessage(String name, String message) {
        for (User user : users) {
            if (!user.getName().equalsIgnoreCase(name)) {
                user.receive(message);
            }
        }
    }
}
